package com.cws.backup.controller;

import com.cws.backup.common.ResponseBean;
import com.cws.backup.entity.BackupEntity;
import com.cws.backup.properties.BackupProperties;
import com.cws.backup.thread.BackupThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * BackUpController 手动备份接口
 *
 * @author cws 18855165128
 * @date 2022/9/17 11:33
 */
@Slf4j
@RestController
public class BackUpController {

    @Autowired
    BackupProperties backupProperties;

    @RequestMapping("/backup")
    public ResponseBean backup() {
        List<BackupEntity> backupTaskList = backupProperties.getTaskList();
        int i = 0;
        for (BackupEntity backupEntity : backupTaskList) {
            //是否启用
            if (Objects.equals(backupEntity.getEnable(), "Y")) {
                BackupThread backupThread = new BackupThread(backupEntity);
                Thread myThread = new Thread(backupThread);
                myThread.start();
                i += 1;
            }
        }
        return ResponseBean.success(i + "个数据库备份完成!");
    }
}
